<?php
/**
 * Project: Recipe
 * File: MenuItemAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-5-7
 * Time: 上午10:43
 */

	class MenuItemAction extends CommonAction{


		function index(){

			if(empty($_GET['mid']) || !isset($_GET['mid'])){
				$this->error('异常操作');
			}

			import("ORG.Util.Page");

			$menuitem = D('MenuItem');

			$field = array('id', 'name', 'pid', 'path', "concat(path, '-', id)"=>'bpath', 'alias', 'type', 'sort', 'publish','howopen');

			if(isset($_POST['search'])){
				if(!empty($_POST['key'])){
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
						case 3 :
							$datas['alias'] = array('like',"%{$_POST['key']}%");
							break;
					}
					$datas['menuid'] = $_GET['mid'];
					$total = $menuitem->where($datas)->count();
					$Page = new Page($total, 20);
					$show = $Page->show();
					$list = $menuitem->relation(true)->where($datas)->field($field)->limit($Page->firstRow.','.$Page->listRows)->order('bpath,id')->select();
				}else{
					$this->error('搜索内容不能为空', __APP__.'/MenuItem/index');
				}
			}else{
				$total = $menuitem->where('menuid='.$_GET['mid'])->count();
				$Page = new Page($total, 20);// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $menuitem->relation(true)->where('menuid='.$_GET['mid'])->field($field)->limit($Page->firstRow.','.$Page->listRows)->order('bpath,id')->select();
			}

			foreach($list as $key=>$value){
				$list[$key]['count'] = count(explode('-', $value['bpath']));
				for($i=0; $i<$list[$key]['count']; $i++){
					$list[$key]['nbsp'].= '&nbsp;&nbsp;';
				}
			}

			$this->assign('list',$list);// 赋值数据集

			$this->assign('show',$show);// 赋值分页输出

			$this->display('index');
		}

		/**
		 * 指向选择组件界面
		 */
		function component(){
			$component = M('Component');
			$componentinfo = $component->where('publish=1')->select();
			$this->assign('componentinfo', $componentinfo);
			$this->display();
		}

		/**
		 * 指向添加菜单项界面
		 */
		function add(){

			$component = M('Component');
			$componentinfo = $component->where('publish=1')->find($_POST['cid']);
			$this->assign('data', $componentinfo);

			$mid = !empty($_POST['mid']) ? $_POST['mid'] : $_GET['mid'];
			$menuitem = D('MenuItem');
			$fields = array('id', 'name', 'pid', 'path',"concat(path, '-', id)"=>'bpath');
			$menuiteminfo = $menuitem->where('menuid='.$mid)->field($fields)->order('bpath,id')->select();

			foreach($menuiteminfo as $key=>$value){
				$menuiteminfo[$key]['count'] = count(explode('-', $value['bpath']));
				for($i=0; $i<$menuiteminfo[$key]['count']; $i++){
					$menuiteminfo[$key]['nbsp'].= '&nbsp;&nbsp;';
				}
			}

			$this->assign('datas', $menuiteminfo);
			$this->display();
		}

		/**
		 * 添加菜单项
		 */
		function insert(){
			$menuitem = D('MenuItem');
			if($data = $menuitem->create()){
			//	$map['menuid'] = $data['menuid'];
			//	$menuiteminfo = $menuitem->where($map)->select();
			//	foreach($menuiteminfo as $key){
			//		$links[]=$key['link'];
			//	}

			//	if(array_key_exists($data['link'], array_flip($links))){
			//		$this->error('该地址已经生成菜单项');
			//	}else{
					if($menuitem->add()){
						$this->success('添加菜单项成功', __APP__.'/MenuItem/index/mid/'.$data['menuid']);
					}else{
						$this->error('添加菜单项失败');
					}
			//	}

			}else{
				$this->error($menuitem->getError(), __APP__.'/MenuItem/add');
			}
		}

		/**
		 *菜单项封面上传
		 */
		function uploads(){
			import("ORG.Net.emit_upload");
			import("ORG.Util.emit_image");
			$upload = new upload(array("filepath"=>"./Public/Content/tmp","allowtype"=>array('gif','png','jpg'),"israndname"=>true,"maxsize"=>2000000));
			if($upload->uploadfiles('pic')){
				$img = new image("./Public/Content/tmp");
				$imgname =  $upload->getnewname();
				$img->thumb($imgname, 132, 75);
				echo $upload->getnewname();
			}else{
				echo $upload->geterrormsg();
			}
		}

		/**
		 * 指向修改菜单项界面
		 */
		function edit(){
			if(isset($_GET['mid']) && !empty($_GET['mid'])){
				if(isset($_GET['id']) && !empty($_GET['id'])){

					$menuitem = D('MenuItem');
					$fields = array('id', 'name', 'pid', 'path',"concat(path, '-', id)"=>'bpath');
					$menuiteminfo = $menuitem->where('menuid='.$_GET['mid'])->field($fields)->order('bpath,id')->select();
					foreach($menuiteminfo as $key=>$value){
						$menuiteminfo[$key]['count'] = count(explode('-', $value['bpath']));
						for($i=0; $i<$menuiteminfo[$key]['count']; $i++){
							$menuiteminfo[$key]['nbsp'].= '&nbsp;&nbsp;';
						}
					}

					$this->assign('datas', $menuiteminfo);

					if(isset($_GET['miid']) && !empty($_GET['miid'])){
						$component = M('Component');
						$componentinfo = $component->where('publish=1')->find($_POST['cid']);
						$this->assign('componentinfo', $componentinfo);
					}

					$menuiteminfos = $menuitem->where('menuid='.$_GET['mid'])->find($_GET['id']);
					$this->assign('data', $menuiteminfos);

					$this->display();

				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}
		}

		/**
		 * 修改菜单项
		 */

		function update(){
			if(isset($_POST['id']) && !empty($_POST['id'])){
				if(isset($_POST['mid']) && !empty($_POST['mid'])){
					$menuitem = D('MenuItem');
					$count = $menuitem->where('menuid='.$_POST['mid'].' AND pid='.$_POST['id'])->count();

					if($data = $menuitem->create()){

						if($data['pid']==$data['id']){
							unset($data['pid']);
							unset($data['path']);
						}else{
							if($count>0){
								$this->error('该菜单项下面还有子菜单项,不能移动位置');
							}
						}
						$menuitem->save($data);
						$this->success('修改菜单项成功',  __APP__.'/MenuItem/index/mid/'.$_POST['mid']);
					}else{
						$this->error($menuitem->getError());
					}
				}else{
					$this->error('异常操作');
				}
			}else{
				$this->error('异常操作');
			}
		}

		/**
		 * 删除菜单项
		 */
		function del(){
			if(!empty($_GET['mid']) && isset($_GET['mid'])){
				if(!empty($_POST['actions'])){
					$putcount = count($_POST['actions']);
					$menuitem = M('MenuItem');
					$innercount = $menuitem->where('menuid='.$_GET['mid'])->count();
					foreach($_POST['actions'] as $key=>$value){
						if($putcount == $innercount){
							$num = $menuitem->delete($value);
						}else{
							$count = $menuitem->where('menuid='.$_GET['mid'].' AND pid='.$value)->count();
							if($count>0){
								$this->error('此菜单项下还有子菜单项,不能删除');
							}else{
								$num = $menuitem->delete($value);
							}
						}
					}
					if($num>0){
						$this->success('删除菜单项成功', __APP__.'/MenuItem/index/mid/'.$_GET['mid']);
					}else{
						$this->error('删除菜单项失败');
					}
				}else{
					$this->error('请先选择菜单项');
				}
			}else{
				$this->error('异常操作');
			}
		}

}